// 从服务器获取各类型消息的未读数量（拆分版）
import {
  getFanUnreadNumAPI,
  getLikeUnreadNumAPI,
  getCommentUnreadNumAPI,
  getAtUnreadNumAPI
} from '@/api/notifications/notifications'

// 1. 获取粉丝相关未读数量
export async function fetchFanUnreadNum(userId) {
  try {
    const res = await getFanUnreadNumAPI(userId);
    return Number(res.data) || 0; // 转换为数字，空值默认0
  } catch (error) {
    console.error('获取粉丝未读数量失败：', error.message);
    return 0; // 失败返回0
  }
}

// 2. 获取点赞相关未读数量
export async function fetchLikeUnreadNum(userId) {
  try {
    const res = await getLikeUnreadNumAPI(userId);
    return Number(res.data) || 0; // 转换为数字，空值默认0
  } catch (error) {
    console.error('获取点赞未读数量失败：', error.message);
    return 0; // 失败返回0
  }
}

// 3. 获取评论相关未读数量
export async function fetchCommentUnreadNum(userId) {
  try {
    const res = await getCommentUnreadNumAPI(userId);
    return Number(res.data) || 0; // 转换为数字，空值默认0
  } catch (error) {
    console.error('获取评论未读数量失败：', error.message);
    return 0; // 失败返回0
  }
}


// 保留原组合函数（内部调用拆分后的独立函数，保持代码复用）
export async function fetchAllMessageUnreadNums(userId) {
  const [
    fanUnreadNum,
    likeUnreadNum,
    commentUnreadNum,
    atUnreadNum
  ] = await Promise.all([
    fetchFanUnreadNum(userId),  // 调用独立函数
    fetchLikeUnreadNum(userId),
    fetchCommentUnreadNum(userId),
    fetchAtUnreadNum(userId)
  ]);
  return {
    fanUnreadNum,
    likeUnreadNum,
    commentUnreadNum,
    atUnreadNum
  };
}